﻿using System;
using System.Text;

namespace BoxSauce.AgencyToolkit.Collections
{
    /// <summary>
    /// Extensions for ByteArrays
    /// </summary>
    public static class ByteArraysExtensions
    {
        /// <summary>
        /// Convert ByteArray to Hex String
        /// </summary>
        /// <param name="data"></param>
        /// <returns>Hex string of bytearray</returns>
        public static string ToHexString(this byte[] data)
        {
            var sb = new StringBuilder(data.Length * 2);
            foreach (byte b in data)
            {
                sb.AppendFormat("{0:x2}", b);
            }
            return sb.ToString();
        }

        /// <summary>
        /// Convert ByteArray to Base64 String
        /// </summary>
        /// <param name="data"></param>
        /// <returns>Base64 string of bytearray</returns>
        public static string ToBase64String(this byte[] data)
        {
            return Convert.ToBase64String(data);
        }

        /// <summary>
        /// Convert ByteArray to UTF8 String
        /// </summary>
        /// <param name="data"></param>
        /// <returns>UTF8 string of bytearray</returns>
        public static string ToUtf8String(this byte[] data)
        {
            return new UTF8Encoding().GetString(data);
        }
    }
}
